import Vue from 'vue';
import {
	Message,
	MessageBox
} from 'element-ui';
import constant from 'util/constant.js';
import caches from 'util/caches.js';
let utils = {
	//消息内容
	msg: {
		success(message, time, callBack) {
			var duration = null;
			if (typeof(arguments[1]) == 'function') {
				callBack = arguments[1];
				duration = 1000;
			} else {
				duration = time == null ? 1000 : time;
			}
			Message({
				duration,
				message,
				type: 'success',
				onClose: callBack
			});
		},
		warning(message, time, callBack) {
			var duration = null;
			if (typeof(arguments[1]) == 'function') {
				callBack = arguments[1];
				duration = 1000;
			} else {
				duration = time == null ? 1000 : time;
			}
			Message({
				duration,
				message,
				type: 'warning',
				onClose: callBack
			});
		},
		info(message, time, callBack) {
			var duration = null;
			if (typeof(arguments[1]) == 'function') {
				callBack = arguments[1];
				duration = 1000;
			} else {
				duration = time == null ? 1000 : time;
			}
			Message({
				duration,
				message,
				type: 'info',
				onClose: callBack
			});
		},
		exception(message, time, callBack) {
			var duration = null;
			if (typeof(arguments[1]) == 'function') {
				callBack = arguments[1];
				duration = 1000;
			} else {
				duration = time == null ? 1000 : time;
			}
			Message({
				duration,
				message,
				type: 'error',
				onClose: callBack
			});
		}
	},
	//询问框
	confirm(title, msg, successCallback, cancelCallback) {
		MessageBox.confirm(msg, title, {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			.then(() => {
				if (successCallback) {
					successCallback();
				}
			})
			.catch(() => {
				if (cancelCallback) {
					cancelCallback
				}
			});
	},
	merger(obj1, obj2) {
		return Object.assign({}, obj1, obj2);
	},
	checkForm(form, success, fail) {
		form.validate((valid) => {
			if (valid) {
				success();
			} else {
				if (fail) {
					fail();
				}
				return false;
			}
		});
	},
	newRequest() {
		return new constant.requestObj();
	},
	newResponse() {
		return new constant.responseObj();
	},
	convertSimpleTreeToStandardTree(items, simpleFormat, standardFormat) {
		function getChild(pid, items) {
			let result = [];
			if (items && items.length > 0) {
				for (let item of items) {
					if (item[simpleFormat.pid] == pid) {
						let obj = {};
						obj[standardFormat.id] = item[simpleFormat.id];
						obj[standardFormat.pid] = item[simpleFormat.pid];
						obj[standardFormat.label] = item[simpleFormat.label];
						obj[standardFormat.children] = getChild(item[simpleFormat.id], items);
						obj = utils.merger(item, obj);
						result.push(obj);
					}
				}
			}
			return result;
		}

		function SimpleFormat() {
			this.id = "id";
			this.label = "label";
			this.pid = "pid";
			this.defaultParent = "";
		}

		function StandardFormat() {
			this.id = "id";
			this.label = "label";
			this.pid = "pid";
			this.children = "children";
		}
		let result = [];
		simpleFormat = utils.merger(new SimpleFormat(), simpleFormat);
		standardFormat = utils.merger(new StandardFormat(), standardFormat);
		if (items && items.length > 0) {
			for (let item of items) {
				if (item[simpleFormat.pid] == simpleFormat.defaultParent) {
					let obj = {};
					obj[standardFormat.id] = item[simpleFormat.id];
					obj[standardFormat.pid] = simpleFormat.defaultParent;
					obj[standardFormat.label] = item[simpleFormat.label];
					obj[standardFormat.children] = getChild(item[simpleFormat.id], items);
					obj = utils.merger(item, obj);
					result.push(obj);
				}
			}
		}
		return result;
	},
	array: {
		//并集
		union(arr1, arr2) {
			return Array.from(new Set([...arr1, ...arr2]));
		},
		//交集
		intersection(arr1, arr2) {
			return arr1.filter(item => new Set(arr2).has(item));
		},
		//补集
		complement(arr1, arr2) {
			return [...arr1.filter(item => !new Set(arr2).has(item)), ...arr2.filter(item => !new Set(arr1).has(item))];
		},
		//差集
		diff(arr1, arr2) {
			return arr1.filter(item => !new Set(arr2).has(item));
		},
		//包含
		includes(arr1, arr2) {
			return arr2.some(val => arr1.includes(val));
		},
		//全包含
		includesAll(arr1, arr2) {
			return arr2.every(val => arr1.includes(val));
		}
	},
	permissions: {
		permission: {
			hasPermission(permission) {
				let permissions = caches.codes.get();
				return permissions.indexOf(permission) > -1
			},
			orPermission(permission) {
				let permissions = caches.codes.get();
				return utils.array.includes(permissions, permission)
			},
			andPermission(permission) {
				let permissions = caches.codes.get();
				return utils.array.includesAll(permissions, permission)
			},
		},
		role: {
			hasRoleId(rid) {
				let rids = caches.user.get().rid;
				if ((typeof rids == 'object') && rids.constructor == Array) {} else {
					rids = [rids];
				}
				let roleChildren = caches.roleChildren.get();
				rids = utils.array.union(rids, roleChildren.map((item) => {
					return item.id
				}));
				return rids.indexOf(rid) > -1
			},
			orRoleId(rid) {
				let rids = caches.user.get().rid;
				if ((typeof rids == 'object') && rids.constructor == Array) {} else {
					rids = [rids];
				}
				let roleChildren = caches.roleChildren.get();
				rids = utils.array.union(rids, roleChildren.map((item) => {
					return item.id
				}));
				return utils.array.includes(rids, rid)
			},
			andRoleId(rid) {
				let rids = caches.user.get().rid;
				if ((typeof rids == 'object') && rids.constructor == Array) {} else {
					rids = [rids];
				}
				let roleChildren = caches.roleChildren.get();
				rids = utils.array.union(rids, roleChildren.map((item) => {
					return item.id
				}));
				return utils.array.includesAll(rids, rid)
			},
			hasRoleName(rname) {
				let rnames = caches.user.get().rname;
				if ((typeof rnames == 'object') && rnames.constructor == Array) {} else {
					rnames = [rnames];
				}
				let roleChildren = caches.roleChildren.get();
				rnames = utils.array.union(rnames, roleChildren.map((item) => {
					return item.name
				}));
				return rnames.indexOf(rname) > -1
			},
			orRoleName(rname) {
				let rnames = caches.user.get().rname;
				if ((typeof rnames == 'object') && rnames.constructor == Array) {} else {
					rnames = [rnames];
				}
				let roleChildren = caches.roleChildren.get();
				rnames = utils.array.union(rnames, roleChildren.map((item) => {
					return item.name
				}));
				return utils.array.includes(rnames, rname)
			},
			andRoleName(rname) {
				let rnames = caches.user.get().rname;
				if ((typeof rnames == 'object') && rnames.constructor == Array) {} else {
					rnames = [rnames];
				}
				let roleChildren = caches.roleChildren.get();
				rnames = utils.array.union(rnames, roleChildren.map((item) => {
					return item.name
				}));
				return utils.array.includesAll(rnames, rname)
			},
		},
		department: {
			hasDepartmentId(did) {
				let dids = caches.user.get().did;
				if ((typeof dids == 'object') && dids.constructor == Array) {} else {
					dids = [dids];
				}
				let departmentChildren = caches.departmentChildren.get();
				dids = utils.array.union(dids, departmentChildren.map((item) => {
					return item.id
				}));
				return dids.indexOf(did) > -1
			},
			orDepartmentId(did) {
				let dids = caches.user.get().did;
				if ((typeof dids == 'object') && dids.constructor == Array) {} else {
					dids = [dids];
				}
				let departmentChildren = caches.departmentChildren.get();
				dids = utils.array.union(dids, departmentChildren.map((item) => {
					return item.id
				}));
				return utils.array.includes(dids, did)
			},
			andDepartmentId(did) {
				let dids = caches.user.get().did;
				if ((typeof dids == 'object') && dids.constructor == Array) {} else {
					dids = [dids];
				}
				let departmentChildren = caches.departmentChildren.get();
				dids = utils.array.union(dids, departmentChildren.map((item) => {
					return item.id
				}));
				return utils.array.includesAll(dids, did)
			},

			hasDepartmentName(dname) {
				let dnames = caches.user.get().dname;
				if ((typeof dnames == 'object') && dnames.constructor == Array) {} else {
					dnames = [dnames];
				}
				let departmentChildren = caches.departmentChildren.get();
				dnames = utils.array.union(dnames, departmentChildren.map((item) => {
					return item.name
				}));
				return dnames.indexOf(dname) > -1
			},
			orDepartmentName(dname) {
				let dnames = caches.user.get().dname;
				if ((typeof dnames == 'object') && dnames.constructor == Array) {} else {
					dnames = [dnames];
				}
				let departmentChildren = caches.departmentChildren.get();
				dnames = utils.array.union(dnames, departmentChildren.map((item) => {
					return item.name
				}));
				return utils.array.includes(dnames, dname)
			},
			andDepartmentName(dname) {
				let dnames = caches.user.get().dname;
				if ((typeof dnames == 'object') && dnames.constructor == Array) {} else {
					dnames = [dnames];
				}
				let departmentChildren = caches.departmentChildren.get();
				dnames = utils.array.union(dnames, departmentChildren.map((item) => {
					return item.name
				}));
				return utils.array.includesAll(dnames, dname)
			},
		}
	},
}

export default utils;
